package com.jumaojiang.controller;

import com.jumaojiang.exceptions.ResourceException;
import com.jumaojiang.service.ParsingResourcesService;
import com.jumaojiang.vo.AjaxResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * javaee-course
 *
 * @author wuhanwei
 * @version 1.0
 * @date 2021/11/12
 */
@Controller
@ResponseBody
@RequestMapping("get-resource")
public class GetingResources {

    @Autowired
    private ParsingResourcesService service;

    @RequestMapping(value = "update-chapter/{chapter}")
    public AjaxResultVo setCookie(@RequestParam("cookie") String cookie, @PathVariable("chapter") Integer chapter) throws ResourceException {
        // 请求获取所有章节信息 & 获取指定章节的id
        String chapterId = service.parseChapterId(cookie, chapter);
        // 请求单章节信息 & 解析章节信息
        List<List<Object>> lists = service.parseChapterMsg(chapterId, cookie, chapter);
        // 请求解析视频m3u
        List<List<Object>> lists1 = null;
        try {
            lists1 = service.parseVideoM3u(lists);
        } catch (Exception e) {
            return new AjaxResultVo(500, e.getMessage());
        }
        // 更新数据库
        Integer res = 0;
        try {
            res = service.updateDB(lists1);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResultVo(500, e.getMessage());
        }
        // 返回结果
        if(res == 0){
            return new AjaxResultVo(500, "更新了个寂寞");
        }
        return new AjaxResultVo(lists1);
    }
}
